package com.caifu.controller;

import com.caifu.bean.Result;
import com.caifu.bean.ResultUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author tww
 * ClassName: LoginController.java、
 * createTime: 2021年03月04日 09:49:34
 **/
@Controller
public class LoginController {

    private static final Logger logger = LoggerFactory.getLogger(LoginController.class);

    @GetMapping(value = {"", "/", "/login", "login"})
    public String login() {
        return "login";
    }

    @PostMapping("login")
    @ResponseBody
    public Result toLogin(String loginName, String password, String reme) {

        //获取主体
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(new UsernamePasswordToken(loginName, password));
            return ResultUtil.success();
        } catch (AuthenticationException e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage());
        }
    }

}
